﻿using ASmile.Com;
using ASmile.DBModel;
using System;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmRoleUrl : FrmBase
    {
        public FrmRoleUrl()
        {
            InitializeComponent();
        }

        IRoleUrlBLL BLLObj { get; } = ObjectFactory.GetObject<IRoleUrlBLL>("RoleUrlBLL");

        ArgMainPageData SearchArg { get; } = new ArgMainPageData();

        void RefreshUrl()
        {
            SetPageData(1, ucPageNav.OnePageCount, out int total);
            ucPageNav.InitiControl(total);
        }

        void RefreshRole()
        {
            var ret = BLLObj.GetRoleData(tsTxtRoleSearch.Text);
            dgvRole.DataSource = ret.data;
        }

        private void FrmRoleUrl_Load(object sender, EventArgs e)
        {
            ucPageNav.PageChangedEvent += SetPageData;
            RefreshRole();
            SetCopyInsertSql<Sys_RoleUrl>(dgvRoleUrl);
        }

        private void SetPageData(int pageNo, int pageSize, out int total)
        {
            var role = dgvRole.GetSelectedClassData<Sys_Role>();
            if (role == null)
            {
                total = 0;
                dgvRoleUrl.DataSource = null;
                return;
            }

            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.MainId = role.Id;
            SearchArg.SearchVal = tsTxtSearch.Text;
            var ret = BLLObj.GetList(SearchArg);
            total = ret.count;
            dgvRoleUrl.DataSource = ret.data;
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsTxtRoleSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshRole();
            }
        }

        private void tsBtnSearchRole_Click(object sender, EventArgs e)
        {
            RefreshRole();
        }

        private void dgvRole_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            RefreshUrl();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            var role = dgvRole.GetSelectedClassData<Sys_Role>();
            if (role == null)
            {
                ShowMessage("请选中角色");
                return;
            }
            Sys_RoleUrl newModel = new Sys_RoleUrl
            {
                RoleId = role.Id,
                RoleName = role.RoleName
            };
            FrmRoleUrlEdit frm = new FrmRoleUrlEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = newModel,
                SaveHandle = m => BLLObj.Add(m as Sys_RoleUrl)
            });
            if (ret)
            {
                newModel.Id = ret.data;
                dgvRoleUrl.AddDataSource(newModel);
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var m = dgvRoleUrl.GetSelectedClassData<Sys_RoleUrl>();
            if (m == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            FrmRoleUrlEdit frm = new FrmRoleUrlEdit();
            var ret =
            frm.ShowDialog(new EditFormArgs
            {
                SetModel = m,
                SaveHandle = model => BLLObj.Update(model as Sys_RoleUrl)
            });
            if (ret)
            {
                dgvRoleUrl.Refresh();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDel_Click(object sender, EventArgs e)
        {
            var m = dgvRoleUrl.GetSelectedClassData<Sys_RoleUrl>();
            if (m == null)
            {
                ShowMessage("未选中记录");
                return;
            }
            if (!ShowQuestion("确实要删除选中规则吗？", "删除确认")) return;

            var ret = BLLObj.Del(m);
            if (ret)
            {
                dgvRoleUrl.DelDataSource(m);
            }
            ShowMessage(ret.msg);
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RefreshUrl();
            }
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            RefreshUrl();
        }

        private void dgvRoleUrl_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
